/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.domain.partner;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import info.peigen.hotpot.business.account.core.domain.account.Account;
import info.peigen.hotpot.business.account.core.util.CreateAccountUtil;
import info.peigen.hotpot.business.customer.facade.enums.partner.PartnerStatus;
import info.peigen.hotpot.business.customer.facade.order.partner.PartnerCreateOrder;
import info.peigen.hotpot.common.core.domain.Domain;
import info.peigen.hotpot.common.core.domain.DomainObject;
import info.peigen.hotpot.common.core.order.Order;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;
import lombok.experimental.SuperBuilder;

import static info.peigen.hotpot.business.customer.facade.enums.partner.PartnerStatus.NORMAL;


/**
 * <b>(Partner)</b>
 * 合作伙伴
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/10/11
 */
@EqualsAndHashCode(callSuper = true)
@SuperBuilder
@Data
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Accessors(chain = true)
public class Partner extends DomainObject {
    /** 合作伙伴Id */
    @NotEmpty
    @Size(max = 24)
    @Builder.Default
    String            partnerId    = CreateAccountUtil.createPartnerAccountId();
    /** 合作伙伴缩写,便于记忆,随机8个字母 */
    @Builder.Default
    String            abbreviation = RandomUtil.randomString(8);
    /** 合作伙伴名字 */
    @NotEmpty
    @Size(max = 24)
    String            partnerName;
    @NotNull
    @Builder.Default
    PartnerStatus     status       = NORMAL;
    /** 权限 */
    @Builder.Default
    PartnerPermission permission   = PartnerPermission.genPartnerInitPermission();
    /** 合作伙伴模块与技术相关的内容 */
    PartnerTech tech;
    /** 中间账户 */
    Account     intermediateAccount;
    /** 自有账户 */
    Account     selfAccount;
    String      memo;

    public static Partner createPartner(PartnerCreateOrder registerOrder) {
        Partner partner = init().setPartnerName(registerOrder.getPartnerName());
        if (ObjectUtil.isNotEmpty(registerOrder.getPartnerId())) {
            partner.setPartnerId(registerOrder.getPartnerId());
        }
        if (ObjectUtil.isNotEmpty(registerOrder.getAbbreviation())) {
            partner.setAbbreviation(registerOrder.getAbbreviation());
        }
        partner.check();
        return partner;
    }

    private static Partner init() {
        return Partner.builder().permission(PartnerPermission.builder().build()).tech(PartnerTech.builder().build()).build();
    }

    @Override
    public Domain create(Order order) {
        PartnerCreateOrder registerOrder = (PartnerCreateOrder) order;
        registerOrder.check();
        return init().setPartnerName(registerOrder.getPartnerName());
    }

    public final static String ReArchCN_PartnerId = "888888888888888888888888";

    public final static Partner RearchPartner() {
        return Partner.builder()
                .partnerId("888888888888888888888888")
                .abbreviation("ReArchCN")
                .partnerName("重庆重新构架科技有限公司")
                .permission(PartnerPermission.getAllAllow())
                .tech(PartnerTech.builder().build()).build();
    }
}
